var a,adr,auth,Delay=50,GM
var mp,dis=[1,1,1,1,1,1,1,1,1,1],b,i,j
var op={weekday:"short",year:"numeric",month:"numeric",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}

var Rel_chg_a,IO_chg_a,Log_chg_a
var tb="<table class=ctrlb cellspacing=7 ondragstart=\"return false;\" ondrop=\"return false;\" border=0 style='font-size:8.5pt;color:#FFF;width:650px'><tr height=30>"
var tb1="<table id='one' cellspacing=7 border=0 ondragstart=\"return false;\" ondrop=\"return false;\" style='background:#bbb;color:#FFF;width:300px'>"

var bg_e= "background-color:#059105' class=bt"
var bg_a= "background-color:#5A4848' class=bt"
var bg_he="background-color:#089DF4' class=bt"
var bg_ha="background-color:#033451' class=bt"
var bg_ee="background-color:#837373;border:2px inset #ddd' class=bt1>"
var bg_ea="background-color:#6F9576;border:2px inset #ddd' class=bt1>"

var newP = document.createElement('p')
var ts,tt
var i_div=100
var cos_div=100
f_av =[0,0,0,0,0]
var fp=0, av, nn, wd=[0,0,0,0,0,0,0,0] ,wi=[0,0,0,0,0,0,0,0]
var zt=0, zk=0

gI = function () {}
gI.id = function (div) { return document.getElementById(div);}
gN = function () {}
gN.n = function (div) { return document.getElementsByName(div)[0];}

function getOS(){
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;
if (macosPlatforms.indexOf(platform) !== -1) { os = 'Mac OS';}
else if (iosPlatforms.indexOf(platform) !== -1) { os = 'iOS';}
else if (windowsPlatforms.indexOf(platform) !== -1) { os = 'Windows';}
else if (/Android/.test(userAgent)) { os = 'Android';}
else if (!os && /Linux/.test(platform)) {os = 'Linux';}
return os;
}

function rdt(){
tg=new XMLHttpRequest()
tg.open("GET","txt/text_"+parent.Lang+'.txt',false)
tg.send("")
parent.ttd = tg.responseText.split("\r\n")
}

function lg(){
l=new XMLHttpRequest()
l.open("GET","txt/lang_"+parent.Lang+'.txt',false)
l.send("")
parent.lang_txt = l.responseText.split("\r\n")
}

function slg(){
if(parent.Lang==0){tt=",";ts="."}
if(parent.Lang==1){tt=".";ts=","}
var nodes= document.body.getElementsByTagName('*'),
L=nodes.length
while(L){
temp= nodes[--L].id || ''
if(temp.indexOf('lg_')== 0){
var nr=parseInt(temp.substring(3,6))
var tmp=gI.id(temp).innerHTML
gI.id(temp).innerHTML=parent.lang_txt[nr]+tmp}}
}

function slg_s(nr){
return parent.lang_txt[nr]
}

function dds(id){
gI.id(id).innerHTML="<span style='color:#990000'>"+slg_s(60)+"</span>"
gI.id("tb1").style.display="none"
gI.id("tb2").style.display="none"
gI.id("tb3").style.display="none"
}

function split_bin(i){
ret=new Array("","","","","","","","","","","","","","","","") 
var bin=parseInt(i).toString(2)
for(var j=0;j<bin.length;j++)
if(bin.substring(bin.length-1-j,bin.length-j)=="1") ret[j]="1"
return ret
}

function ok_fail_res(){
gI.id("ok").style.display="none"
gI.id("fail").style.display="none"
}

function sc(t,f) {
var f=document.forms[f];
for(var i=0;i<f.elements.length;i++){
if(f.elements[i].getAttribute('onchange')==null)
{f.elements[i].onchange=function(){gN.n(t).style.color='#CC0066'}}}
}

function nf( x, len, sep, ts){
x = Math.round( x * Math.pow(10, len) ) / Math.pow(10, len);
str_no = x+"";
arr_int = str_no.split(".");
if(!arr_int[0]) arr_int[0] = "0";
if(!arr_int[1]) arr_int[1] = "";
if(arr_int[1].length < len){
nk = arr_int[1];
for(i=arr_int[1].length+1; i <= len; i++){  nk += "0";  }    arr_int[1] = nk;}
if(ts != "" && arr_int[0].length > 3){
b = arr_int[0];
arr_int[0] = "";
for(j = 3; j < b.length ; j+=3){
Extrakt = b.slice(b.length - j, b.length - j + 3);
arr_int[0] = ts + Extrakt +  arr_int[0] + "";}
str_first = b.substr(0, (b.length % 3 == 0)?3:(b.length % 3));
arr_int[0] = str_first + arr_int[0];}
return arr_int[0]+sep+arr_int[1];
}

function p_tm(t,typ){
if(typ==0)	// hh:mm
	tim=aLZ( t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)	
if(typ==1)	// hh:mm:ss
	tim=aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==2)	// tt.mm.jjjj, hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"."+aLZ(t.getUTCMonth()+1,2)+"."+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==3)	// tt.mm.jjjj hh-mm-ss
	tim=aLZ(t.getUTCDate(),2)+"-"+aLZ(t.getUTCMonth()+1,2)+"-"+t.getUTCFullYear()+" "+aLZ(t.getUTCHours(),2)+"."+aLZ(t.getUTCMinutes(),2)+"."+aLZ(t.getUTCSeconds(),2)	
if(typ==4)	// WT tt.mm.jjjj, hh:mm:ss
	tim=parent.lang_txt[t.getUTCDay()+255]+" "+aLZ(t.getUTCDate(),2)+"."+aLZ(t.getUTCMonth()+1,2)+"."+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==5)	// tt/mm/jjjj hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear()+" "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)
if(typ==6)	// WT tt/mm/jjjj, hh:mm:ss english
	tim=parent.lang_txt[t.getUTCDay()+255]+" "+aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==7)	// tt/mm/jj hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear().toString().substr(-2)+" "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)
return tim
}

function aLZ(n,l){
var num = '' + n
while (num.length < l) num = '0' + num
return num
}

function repl(x){
var a=x.toString()
if(parent.Lang==0){
a=a.replace('.','')
a=a.replace(',','.')}
if(parent.Lang==1){
a=a.replace(',','')
a=a.replace('.','.')}
return a
}

function l_0(v){
var nz=16-v.length
if(nz) return Array(nz+1).join("0")+v 
return v
}

function B(b){
return Boolean(Number(b))
}

function init(){
xhttp=new XMLHttpRequest()
xhttp.open("GET","start.cfg",false)
xhttp.send("")
var p = xhttp.responseText.split(";") 

if(p[9]=='end'){
parent.Name=p[0]
newP.innerHTML=p[1]
parent.Name_Func=newP.innerHTML
parent.IP=p[2]
parent.Port=p[3]
var ll=p[4].split(",") 
parent.Login="<b>"+ll[0]+"</b> von "+ll[1]
var pp=p[5].split(",") 
parent.Typ=pp[0]
parent.PowMes=pp[1]
parent.PowMesCal=pp[2]
parent.Ver=p[6]
parent.alw=parseInt(p[7]).toString(2)}
parent.Lang=p[8]
}

function tm(){
xhttp=new XMLHttpRequest()
xhttp.open("GET","daten.cfg",false)
xhttp.send("")
var p = xhttp.responseText.split(";") 
if(p[10]=='end'){
parent.a_time=p[0]*1000
parent.Rel_chg=p[1]
parent.IO_chg=p[2]
parent.Log_chg=p[3]
parent.ClockStat=p[4]
parent.BootTime=parseInt(p[5])+parseInt(p[6])*65536
if(parent.Typ=='o' || parent.Typ=='f')
parent.Temp=nf(parseFloat(p[7]/10),1,tt,ts)
else
parent.Temp=nf(parseFloat(p[7]),1,tt,ts)
parent.Log_o=p[8]
parent.Log_t=p[9]

parent.S_ON=p[11]
if(p[11]=='s'){
parent.Sens_Beg=p[12]
if(p[13]==parseFloat(128.86)) p[13]='---'; else p[13]=nf(parseFloat(p[13]),2,tt,ts)+' °C'
if(p[14]==parseFloat(113.0))  p[14]='---'; else p[14]=nf(parseFloat(p[14]),1,tt,ts)+' %'
parent.S_Temp=p[13]
parent.S_Hum=p[14]
parent.S_Lux=p[15]+' lx'
parent.First_Sens=p[16]
parent.Temp_sw=p[17]}
else {
parent.V=nf(parseFloat(p[11]/10),1,tt,ts)
parent.I=nf(parseFloat(p[12]/10000),4,tt,ts)
parent.Hz=nf(parseFloat(p[13]/1000),3,tt,ts)
parent.W=nf(parseFloat(p[14]/100),2,tt,ts)
parent.VA=nf(parseFloat(p[15]/100),2,tt,ts)
parent.VAR=nf(parseFloat(p[16]/100),2,tt,ts)
parent.cosf=nf(parseFloat(p[17]/32768),4,tt,ts)
parent.Power_Event=p[18]

parent.Vd=parseFloat(p[11]/10).toFixed(1)
parent.Id=parseFloat(p[12]/100).toFixed(2)
parent.Hzd=parseFloat(p[13]/1000).toFixed(3)
parent.Wd=parseFloat(p[14]/100).toFixed(2)
parent.VAd=parseFloat(p[15]/100).toFixed(2)
parent.VARd=parseFloat(p[16]/100).toFixed(2)
parent.cosfd=parseFloat((p[17]/32768)*100).toFixed(2)
parent.tmp_t=p[19]
parent.rnd_t=p[20]}}
}

//first power read
function f_pow_read(){

pd=new XMLHttpRequest()
pd.open("GET","pow_d.cfg",false)
pd.send("")
var dp=pd.responseText.split(";")

parent.sn=dp[1]

for(var i=0;i<60;i++){
parent.V_ar[i]=parseFloat(dp[i*7+2]/10).toFixed(1) 
parent.I_ar[i]=parseFloat(dp[i*7+3]/100).toFixed(2) 
parent.Hz_ar[i]=parseFloat(dp[i*7+4]/1000).toFixed(3)
parent.W_ar[i]=parseFloat(dp[i*7+5]/100).toFixed(2)
parent.VA_ar[i]=parseFloat(dp[i*7+6]/100).toFixed(2)
parent.VAR_ar[i]=parseFloat(dp[i*7+7]/100).toFixed(2)
parent.cosf_ar[i]=parseFloat((dp[i*7+8]/32768)*100).toFixed(2)

var z = new Date((dp[0]-60)*1000);
var tmd = z.getSeconds();

if(!tmd){
tmd=p_tm(z,0)
parent.label_ar[i]=tmd	}
else if(tmd % 15==0)
parent.label_ar[i]=tmd
else	 
parent.label_ar[i]=""
dp[0]++;

parent.time_ar[i]=p_tm(z,5)
parent.time_s_ar[i]="  "+p_tm(z,1) } 
}

function s(t,adr,auth){
xhttps=new XMLHttpRequest()
xhttps.open("POST",adr+"ctrl.htm"+auth,false)
xhttps.setRequestHeader("Content-type","text/plain")
if(t=='h') xhttps.send('h=0'); else xhttps.send(t)
}

function rel_r(adr,auth){
xhttpe=new XMLHttpRequest()
xhttpe.open("GET",adr+"strg.cfg"+auth,false)
xhttpe.send("")
a = xhttpe.responseText.split(";")
}

function io_r(adr,auth){
try{xhttpi=new XMLHttpRequest()
xhttpi.open("GET",adr+"ioa.cfg"+auth,false)
xhttpi.send("")
a = xhttpi.responseText.split(";")} catch(e){return false}
}

function sw_F(i,b){
b.style.boxShadow="2px 2px 2px #fff";	
b.style.textShadow="2px 2px 2px rgba(0,0,0,0.5)"
Rel_chg_a++
s('F'+i+'=S','','')
del_rel()
}
function sw_f(i){
if(wd[i]>0){wd[i]=0
Rel_chg_a++
s('f'+i+'=S','','')
del_rel()}	
}
function sw_g(i){
if(wd[i]==0){wd[i]++
Rel_chg_a++
s('g'+i+'=S','','')
del_rel()}	
}
function sw_E(i,b){
b.style.boxShadow="2px 2px 2px #fff";	
b.style.textShadow="2px 2px 2px rgba(0,0,0,0.5)"
IO_chg_a++
s('E'+i+'=S','','')
del_io()
}
function sw_e(i){
if(wi[i]>0){wi[i]=0
IO_chg_a++
s('e'+i+'=S','','')
del_io()}	
}
function sw_d(i){
if(wi[i]==0){wi[i]++
IO_chg_a++
s('d'+i+'=S','','')
del_io()}
}

function sw(i){
/*
if(i==0) gI.id('lg_0').innerHTML+="onmousedown "	
if(i==1) gI.id('lg_0').innerHTML+="onmouseup "	
if(i==2) gI.id('lg_0').innerHTML+="ontouchstart "	
if(i==3) gI.id('lg_0').innerHTML+="ontouchcancel "	
if(i==4) gI.id('lg_0').innerHTML+="ontouchend "	
if(i==5) gI.id('lg_0').innerHTML+="ontouchleave "	
if(i==6) gI.id('lg_0').innerHTML+="onmouseover "	
*/
}

function r_read(adr,auth){
var b,i
rel_r(adr,auth)
var t='', sf=0
if(parent.OS=="Android" || parent.OS=="iOS") t="2>"; else t=">" 
if(parent.ttd===undefined)rdt()
if(a[40]=='end'){
if(parent.Typ=='o' || parent.Typ=='f') b=tb1; else b=tb	
for(i=0;i<8;i++){parent.d_alw[i]=a[i*5+2];if(parent.d_alw[i]==0&&sf==0){parent.tab_nr=i;sf=1}
if(!(i>0 && (parent.Typ=='o' || parent.Typ=='f'))){
if(a[i*5+2]==1){b+="<td id=rbt"+i+" title='"+parent.lang_txt[25]+"' style='";if(a[i*5+1]==1)b+=bg_ea; else b+=bg_ee;}
else{var N=a[i*5+4]
var Nr=a[i*5+3];Nr++
b+="<td id=rbt"+i+" title='"+parent.ttd[Nr]+"\n"
if(N.match('T') && N.match('K')){b+=parent.lang_txt[26];zt=1;zk=1} else if(N.match('T')){b+=parent.lang_txt[27];zt=1} else if(N.match('K')){b+=parent.lang_txt[28];zk=1}
if(a[i*5+2]==1) b+="'"
else{if(a[i+41]==1){
if(parent.OS=="iOS")b+="' ontouchstart=\"sw(2);sw_g("+i+")\" onmouseup=\"sw(1);sw_f("+i+")\""
else if(parent.OS=="Android") b+="' ontouchstart=\"sw(2);sw_g("+i+")\" ontouchend=\"sw(4);sw_f("+i+")\""
else b+="' onmousedown=\"sw(0);sw_g("+i+")\" onmouseup=\"sw(1);sw_f("+i+")\""}
else b+="' onmousedown=\"sw(0);sw_F("+i+",this)\""}
b+=" style='";if(N.match('B'))b+='box-shadow: 0 0 0 2px #fff inset;';
if(a[i+41]==1){if(a[i*5+1]==1) b+=bg_he+t; else b+=bg_ha+t} else {if(a[i*5+1]==1) b+=bg_e+t; else b+=bg_a+t}}
b+="<div class=bbt>"+a[i*5]+"</div>"
if(a[i+49]!='0')b+="<div class=bt_div style=\"font-size:27px\">&#"+(a[i+49])+";</div>"
if(zt && zk){b+="<div class=bt_div>TK</div>";zt=0;zk=0}
else if(zt){b+="<div class=bt_div>T</div>";zt=0}
else if(zk){b+="<div class=bt_div>K</div>";zk=0}
if(i==3)b+="<tr height=30>"}}
b+="</table>" 
document.getElementById('rel_z').innerHTML=b}
}

function io_read(adr,auth){
var b,i;a=""
io_r(adr,auth)
var t=''
if(parent.OS=="Android" || parent.OS=="iOS") t="2>"; else t=">" 
if(a.length==1) return
try{document.getElementById('io_div').style.display="inline"}catch(e){}
if(a[33]=='end'){
if(parent.Typ=='o') b=tb1; else b=tb	
for(i=0;i<8;i++){
if(!(i>0 && parent.Typ=='o')){
var s=B(a[i*4+2]),ea=B(a[i*4+3]),inv=B(a[i*4+4])
b+="<td title='"
if(!ea){b+=parent.lang_txt[29]
if(inv){b+="\n"+parent.lang_txt[30];s=!s}
if(a[i+34]==1){if(parent.OS=="iOS")b+="' ontouchstart=\"sw(2);sw_d("+i+")\" onmouseup=\"sw(1);sw_e("+i+")\""
else if(parent.OS=="Android") b+="' ontouchstart=\"sw(2);sw_d("+i+")\" ontouchend=\"sw(4);sw_e("+i+")\""
else b+="' onmousedown=\"sw_d("+i+")\" onmouseup=\"sw_e("+i+")\""}
else b+="' onmousedown=\"sw_E("+i+",this)\""
b+=" style='";if(a[i+50]=='1')b+='box-shadow: 0 0 0 2px #fff inset;';
if(a[i+34]==1){if(s) b+=bg_he+t; else b+=bg_ha+t} else {if(s) b+=bg_e+t; else b+=bg_a+t}}
else{b+=parent.lang_txt[31]
if(inv){b+="\n"+parent.lang_txt[30];s=!s}
b+="' style='"
if(s) b+=bg_ee; else b+=bg_ea}
b+="<div class=bbt>"+a[i*4+1]+"</div>" 
if(a[i+42]!='0')b+="<div class=bt_div style=\"font-size:27px\">&#"+(a[i+42])+";</div>"
if(i==3)b+="<tr height=30>"}}
b+="</table>" 
document.getElementById('io_z').innerHTML=b}
}

function del_rel(){window.setTimeout("r_read('','')",Delay)}
function del_io(){window.setTimeout("io_read('','')",Delay)}
function ref(){rel_ch();io_ch()}
function rel_ch(){if(parent.Rel_chg!=Rel_chg_a){Rel_chg_a=parent.Rel_chg;parent.rel_changed=1;r_read('','')}}
function io_ch(){if(parent.IO_chg!=IO_chg_a){IO_chg_a=parent.IO_chg;io_read('','')}}
